package indexer;
import java.io.*;
import java.util.*;

public class TextFileReader {
	private File file;
	private FileReader fr;
	private BufferedReader bfr;
	private Vector<WordUnit> vec=new Vector<WordUnit>();
	
	public TextFileReader (String fileName){
		file=new File(fileName);		
		try{
			fr=new FileReader(file);
			bfr=new BufferedReader(fr);
		}catch(FileNotFoundException fnfe){
			System.out.println("can not open: " + file.getAbsolutePath());
			System.out.println("the error occurs when initialize " +
					"TextFileReader with " + fileName);
		}
	}

	public void getWords() throws IOException {
		String strall=new String();
		String tempstr=new String();
		tempstr=bfr.readLine();
		
		while (tempstr!=null){
			strall=strall.concat(tempstr);
			strall=strall.concat("\n");
			tempstr=bfr.readLine();
		}
		//System.out.println(strall);
		StringTokenizer st=new StringTokenizer(strall," \t\n\r\f\\");		
		int n=st.countTokens();
		//System.out.println("there are " + n + "words in all");
		for (int i=0;i<n;i++){
			WordUnit wu=new WordUnit();
			wu.aString=st.nextToken();
			wu.offSet=i;
			vec.add(wu);
		}	
	}
	public void standardize() {
		int n=vec.size();
		int temp1,temp2;
		String str;
		Character chr;
here:	for (int i=0;i<n;i++){
			str=vec.elementAt(i).aString;
			//System.out.println(str);
			temp1=str.length()-1;
			chr=str.charAt(temp1);
			while (chr.equals('.')||chr.equals('\'')||
					chr.equals('?')||chr.equals('!')||
					chr.equals('\"')||chr.equals('-')||
					chr.equals(')')||chr.equals(':')||
					chr.equals(',')){
				temp1--;
				if (temp1<0){
					vec.elementAt(i).aString=null;
					continue here;
				}
				chr=str.charAt(temp1);
			}
			temp2=0;
			chr=str.charAt(temp2);
			while (chr.equals('(')||chr.equals('\'')||
					chr.equals('\"')){
				temp2++;
				chr=str.charAt(temp2);				
			}
			str=str.substring(temp2, temp1+1);
			str=str.toLowerCase();
			vec.elementAt(i).aString=str;
		}	
	}
	public Vector<WordUnit> returnWords (){
		Vector<WordUnit> vvec=new Vector<WordUnit>(vec.size());
		Enumeration<WordUnit> en=vec.elements();
		while (en.hasMoreElements()){
			vvec.add(en.nextElement());
		}
		return vvec;
	}			
}
